package com.ytjj.qmyx.supplychain.service;

import com.github.pagehelper.PageHelper;
import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.exception.ApiException;
import com.ytjj.qmyx.supplychain.common.model.request.FileRequest;
import com.ytjj.qmyx.supplychain.common.model.request.QueryOrderRecordRequest;
import com.ytjj.qmyx.supplychain.common.model.request.UploadFileStatusRequest;
import com.ytjj.qmyx.supplychain.common.model.request.UploadRecordRequest;
import com.ytjj.qmyx.supplychain.common.model.response.BankerExportRecordResponse;
import com.ytjj.qmyx.supplychain.mapper.BankerExportRecordMapper;
import com.ytjj.qmyx.supplychain.mapper.UploadRecordMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxUploadRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class UploadRecordService {
    @Resource
    private UploadRecordMapper uploadRecordMapper;
    @Resource
    private BankerExportRecordMapper bankerExportRecordMapper;

    public void saveUploadFileRecord(FileRequest fileRequest) {
        YxUploadRecord yxUploadRecord = new YxUploadRecord();
        yxUploadRecord.setBankerId(fileRequest.getBankerId());
        yxUploadRecord.setFileName(fileRequest.getName());
        yxUploadRecord.setFileUrl(fileRequest.getUrl());
        yxUploadRecord.setUploadTime(new Date());
        uploadRecordMapper.insertSelective(yxUploadRecord);
    }

    public CommonPage getUploadFileRecordList(UploadRecordRequest uploadRecordRequest) {
        PageHelper.startPage(uploadRecordRequest.getPageNum(), uploadRecordRequest.getPageSize());
        List<YxUploadRecord> yxUploadRecordList = null;
        if (uploadRecordRequest.getLevel() == 0) {
            yxUploadRecordList = Optional.ofNullable(uploadRecordMapper.selectByExample(new Example(YxUploadRecord.class)))
                    .orElse(new ArrayList<>());

        } else {
            Example example = new Example(YxUploadRecord.class);
            example.createCriteria().andEqualTo("bankerId", uploadRecordRequest.getBankerId());
            example.orderBy("uploadTime").desc();
            yxUploadRecordList = uploadRecordMapper.selectByExample(example);
        }
        CommonPage commonPage = CommonPage.restPage(yxUploadRecordList);
        commonPage.setList(yxUploadRecordList);
        return commonPage;
    }

    public void changeUploadFileDownloadStatus(UploadFileStatusRequest uploadFileStatusRequest) {
        YxUploadRecord yxUploadRecord = Optional.ofNullable(uploadRecordMapper.selectByPrimaryKey(uploadFileStatusRequest.getId()))
                .orElseThrow(() -> new ApiException("该条记录不存在！"));
        yxUploadRecord.setExtra("1");
        uploadRecordMapper.updateByPrimaryKeySelective(yxUploadRecord);
    }

    public CommonPage getBankerExportRecordList(QueryOrderRecordRequest queryOrderRecordRequest) {
        PageHelper.startPage(queryOrderRecordRequest.getPageNum(), queryOrderRecordRequest.getPageSize());
        List<BankerExportRecordResponse> bankerExportRecordResponses = bankerExportRecordMapper.selectBankerExportRecord(queryOrderRecordRequest.getBankerId(),1);
        CommonPage commonPage = CommonPage.restPage(bankerExportRecordResponses);
        return commonPage;
    }
}
